package com.efast.dmom.equipment.controller.repair;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.dmom.equipment.bean.repair.EquipPartBean;
import com.efast.dmom.equipment.bean.repair.EquipRepairInfoBean;
import com.efast.dmom.equipment.bean.repair.EquipRepairOrderBean;
import com.efast.dmom.equipment.bean.repair.EquipRepairOrderRequestBean;
import com.efast.dmom.equipment.bean.repair.EquipRepairOrderReturnBean;
import com.efast.dmom.equipment.bean.repair.EquipRepairPlanBean;
import com.efast.dmom.equipment.bean.repair.EquipRepairerBean;
import com.efast.dmom.equipment.bean.repair.EquipSpareBean;
import com.efast.dmom.equipment.bean.repair.EquipSpareConsumptionBean;
import com.efast.dmom.equipment.service.repair.IEquipRepairOrderService;
import com.efast.dmom.equipment.service.repair.IEquipRepairPlanService;

/**
 * 
 * @author 作者 Aaron Liu:
 * 
 * @version 创建时间：2019年8月13日 下午4:27:55
 * 
 *          类说明
 * 
 */
@Controller
@RequestMapping("/equipment")
public class EquipRepairController{

	@Autowired
	private IEquipRepairOrderService orderService;

	@Autowired
	private IEquipRepairPlanService planService;

	
	@RequestMapping(value = "/repair/deleteRepairOrders/{id}", method = RequestMethod.POST)
	public @ResponseBody void deleteRepairOrders(@PathVariable Integer id) {
		orderService.deleteRepairOrder(id);
	}

	
	@RequestMapping(value = "/repair/saveRepairPlan", method = RequestMethod.POST)
	public @ResponseBody void saveRepairPlan(@RequestBody EquipRepairPlanBean planBean) {

		planService.saveRepairPlan(planBean);
	}

	
	@RequestMapping(value = "/repair/createRepairOrder", method = RequestMethod.POST)
	public @ResponseBody void createRepairOrder(@RequestBody EquipRepairPlanBean planBean) {

		planService.createRepairOrder(planBean);
	}

	
	@RequestMapping(value = "/repair/getUserByGroupName", method = RequestMethod.POST)
	public @ResponseBody List<EquipRepairerBean> getUserByGroupName(@RequestBody String groupName) {

		List<EquipRepairerBean> personList = orderService.getUserByGroupName(groupName);

		for (EquipRepairerBean person : personList) {
			person.setChecked(false);
		}

		return personList;
	}

	
	@RequestMapping(value = "/repair/updateRepairOrderStatus", method = RequestMethod.POST)
	public @ResponseBody void updateRepairOrderStatus(@RequestBody EquipRepairOrderBean orderBean) {
		planService.updateRepairPlanStatus(orderBean.getPlanId(), orderBean.getStatus());
		orderService.updateRepairOrderStatus(orderBean.getOrderId(), orderBean.getStatus());
	}

	
	@RequestMapping(value = "/repair/getRepairOrderByStatus", method = RequestMethod.POST)
	public @ResponseBody EquipRepairOrderReturnBean getRepairOrderByStatus(
			@RequestBody EquipRepairOrderRequestBean requestBean) {

		return orderService.getRepairOrderByStatus(requestBean);
	}

	
	@RequestMapping(value = "/repair/saveRepairOrder", method = RequestMethod.POST)
	public @ResponseBody void saveRepairOrder(@RequestBody EquipRepairOrderBean orderBean) {

		orderService.saveRepairOrder(orderBean);
	}

	
	@RequestMapping(value = "/repair/getEquipSpare", method = RequestMethod.POST)
	public @ResponseBody List<EquipSpareBean> getEquipSpare(@RequestBody String partName) {

		return orderService.getEquipSpare(partName);
	}

	@RequestMapping(value = "/repair/getRepairOrderDetails", method = RequestMethod.POST)
	public @ResponseBody EquipRepairOrderBean getRepairOrderDetails(
			@RequestBody EquipRepairOrderRequestBean requestBean) {

		return orderService.getRepairOrderDetails(requestBean.getOrderId());
	}

	
	@RequestMapping(value = "/repair/updateRepairPlan", method = RequestMethod.POST)
	public @ResponseBody void updateRepairPlan(@RequestBody EquipRepairPlanBean planBean) {

		planService.updateRepairPlan(planBean);
	}

	
	@RequestMapping(value = "/repair/getEquipPart", method = RequestMethod.POST)
	public @ResponseBody List<EquipPartBean> getEquipPart(@RequestBody String category) {
		
		return orderService.getEquipPart(category);
	}

	
	@RequestMapping(value = "/repair/getEquipInfoByPlanId/{planId}", method = RequestMethod.POST)
	public @ResponseBody EquipRepairPlanBean getRepairPlanBaseInfoById(@PathVariable Integer planId) {
		
		return planService.getRepairPlanBaseInfoById(planId);
	}

	
	@RequestMapping(value = "/repair/finishRepairOrder", method = RequestMethod.POST)
	public @ResponseBody void finishRepairOrder(@RequestBody Integer orderId) {
		
		EquipRepairOrderBean order = orderService.getRepairOrderDetails(orderId);
		if(order != null){
			
			List<EquipSpareConsumptionBean> consumptionList = order.getComsuptionList();
			
			for(EquipSpareConsumptionBean consumption : consumptionList){
				
				Integer count = consumption.getConsumption();
				String spareName = consumption.getSpareName();
				
				orderService.updateSpareInventory(spareName, count);
			}
		}
		orderService.updateRepairOrderStatus(order.getOrderId(), "finished");
		
		planService.updateRepairPlanStatus(order.getPlanId(), "finished");
	}
	
	
	@RequestMapping(value = "/repair/deleteRepairPlan", method = RequestMethod.POST)
	public @ResponseBody void deleteRepairPlan(@RequestBody EquipRepairPlanBean planBean) {

		planService.deleteRepairPlan(planBean.getId());
	}

	
	@RequestMapping(value = "/repair/getRepairInfoByPlanId/{id}", method = RequestMethod.POST)
	public @ResponseBody List<EquipRepairInfoBean> getRepairInfoByPlanId(@PathVariable Integer id) {
		
		return planService.getRepairInfoByPlanId(id);
	}
	
	
	@RequestMapping(value = "/repair/getSpareInfoByPlanId/{id}", method = RequestMethod.POST)
	public @ResponseBody List<EquipSpareConsumptionBean> getSpareInfoByPlanId(@PathVariable Integer id) {
		
		return planService.getSpareInfoByPlanId(id);
	}

}
